package DatabasePack;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author abdosalah
 */
public class ATM {

    private String atm_id;
    private String city;
    private String region;
    private int money;

    public  int no_of_ATM() {
        try {
            int counter = 1;
            // Statement stat = conection.createStatement();
            String sql1 = "select * from ATM";
            ResultSet re1 = DatabaseConnection.statement.executeQuery(sql1);
            while (re1.next()) {
                counter++;
            }
            return counter;
        } catch (SQLException e) {
            System.out.println("no function");
            return 0;
        }
    }

    public  String[][] ATMs() {
        int i = 0;
        String[][] atm = new String[no_of_ATM() - 1][3];
        String sql = "select * from ATM";
        ResultSet re;
        try {
            re = DatabaseConnection.statement.executeQuery(sql);
            while (re.next()) {
                atm[i][0] = "" + re.getString("ATM_ID");
                atm[i][1] = re.getString("CITY");
                atm[i][2] = re.getString("REGION");
                i++;
            }
            return atm;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean  InsertNewATM(String atm_id, String city, String region, int money) {
        PreparedStatement prstmt = null;
        String sql = "insert into ATM values(?,?,?,?)";
        try {
            prstmt = DatabaseConnection.connection.prepareStatement(sql);
            prstmt.setString(1, atm_id);
            prstmt.setString(2, city);
            prstmt.setString(3, region);
            prstmt.setInt(4, money);
            prstmt.executeUpdate();
            return true;
        } catch (Exception e) {
            System.out.println("ERROR this ATM already exist");
            return false;
        }
    }
    public static int GetATMMoney(String atm_id)
    {
       String sql = "select * from ATM where ATM_ID = ";
       sql = sql +"'"+ atm_id+"'";
       ResultSet resultset;
       try{
      resultset = DatabaseConnection.statement.executeQuery(sql);
      resultset.next();
      return resultset.getInt("MONEY");
       }catch(Exception e){System.out.println("getatmoney");e.printStackTrace();return -1;}
       }
    public static boolean check_ATM_Money(String atm_id , int money)
    {
        if(GetATMMoney(atm_id)>money)
        return true;
        else
            return false;
    }
}
